package org.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.dto.MovieTemplateDTO;
import org.chen.exception.ServiceException;
import org.chen.response.R;
import org.chen.service.impl.MovieTemplateService;
import org.chen.template.MovieTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/movie/template")
@RequiredArgsConstructor
@Slf4j
public class MovieTemplateController {
    
    private final MovieTemplateService templateService;

    @PostMapping("/page")
    public R<IPage<MovieTemplateDTO>> getTemplatePage(@RequestBody @Validated MovieTemplateDTO.PageQuery query) {
        Page<MovieTemplate> page = new Page<>(query.getCurrent(), query.getSize());
        IPage<MovieTemplate> pageResult = templateService.getTemplateList(page, query.getTemplateName());
        return R.success(convertToDTO(pageResult));
    }

    @PostMapping
    public R<Void> addTemplate(@RequestBody @Validated MovieTemplateDTO dto) {
        try {
            MovieTemplate template = new MovieTemplate();
            BeanUtils.copyProperties(dto, template);
            templateService.addTemplate(template);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public R<Void> updateTemplate(
            @PathVariable Long id,
            @RequestBody @Validated MovieTemplateDTO dto) {
        try {
            MovieTemplate template = new MovieTemplate();
            BeanUtils.copyProperties(dto, template);
            template.setId(id);
            templateService.updateTemplate(template);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public R<Void> deleteTemplate(@PathVariable Long id) {
        try {
            templateService.deleteTemplate(id);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PutMapping("/{id}/status")
    public R<Void> toggleStatus(
            @PathVariable Long id,
            @RequestParam Boolean enabled) {
        try {
            templateService.toggleTemplateStatus(id, enabled);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/validate")
    public R<Boolean> validateTemplate(@RequestBody @Validated MovieTemplateDTO.ValidateRequest request) {
        return R.success(templateService.validateTemplateContent(request.getTemplateContent()));
    }

    // DTO转换方法
    private IPage<MovieTemplateDTO> convertToDTO(IPage<MovieTemplate> page) {
        return page.convert(template -> {
            MovieTemplateDTO dto = new MovieTemplateDTO();
            BeanUtils.copyProperties(template, dto);
            return dto;
        });
    }

    @PostMapping("/batch/delete")
    public R<Void> batchDelete(@RequestBody List<Long> ids) {
        try {
            templateService.batchDeleteTemplates(ids);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/batch/toggle")
    public R<Void> batchToggleStatus(
            @RequestBody List<Long> ids,
            @RequestParam Boolean enabled
    ) {
        try {
            templateService.batchToggleStatus(ids, enabled);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }
}